package managerGUI;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import managerService.ManagerSystem;
import model.Project;
import model.Type;

public class CreateProjectPopup {

	private JTextField inputName;
	private JTextArea description;
	private DefaultComboBoxModel<Type> typeModel;
	
	private static final int TEXT_LENGTH = 50;

	public CreateProjectPopup(Project p){
		JPanel inputArea = new JPanel(new GridLayout(0, 1));
		
		// premiere ligne de la fenetre
		JPanel firstLine = new JPanel(new BorderLayout(2,5));
		inputName = new JTextField(TEXT_LENGTH);
		final FlowLayout sizeControler = new FlowLayout(FlowLayout.TRAILING, 0, 20);
		final JPanel inputNamePanel = new JPanel(sizeControler);
		inputNamePanel.add(inputName);
		firstLine.add(new JLabel("Project name : "), BorderLayout.WEST);
		firstLine.add(inputNamePanel, BorderLayout.CENTER);
		inputArea.add(firstLine);
		
		// deuxieme ligne de la fenetre
		JPanel secondLine = new JPanel(new BorderLayout(2,5));
		description = new JTextArea(3, 1);
		final JScrollPane descriptionPanel = new JScrollPane(description);
		secondLine.add(new JLabel("Project description:"), BorderLayout.WEST);
		secondLine.add(descriptionPanel, BorderLayout.CENTER);
		inputArea.add(secondLine);
		
		// troisieme ligne de la fenetre
		JPanel thirdLine = new JPanel(new BorderLayout(25, 20));
		List<Type> types = ManagerSystem.getService().getTypes();
		typeModel = new DefaultComboBoxModel<Type>(types.toArray(new Type[types.size()]));
		JComboBox<Type> combo = new JComboBox<Type>(typeModel);
		
		JButton addTypeButton = new JButton("new type");
		addTypeButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				String name = JOptionPane.showInputDialog("name of the new type : ");
				if (name != null && !name.equals("")){
					Type type = new Type(name);
					ManagerSystem.getService().addType(type);
					typeModel.addElement(type);
				}
			}
		});
		addTypeButton.setToolTipText("add a new type");
		
		thirdLine.add(new JLabel("Choose a type : "), BorderLayout.WEST);
		thirdLine.add(combo, BorderLayout.CENTER);
		thirdLine.add(addTypeButton,BorderLayout.EAST);
		inputArea.add(thirdLine);
		
		try{
			while (true){
				int option = JOptionPane.showConfirmDialog(MainInterface.instance,
						inputArea,
						"Create a new project",
						JOptionPane.OK_CANCEL_OPTION,
						JOptionPane.PLAIN_MESSAGE,
						null);
				
				if (option == JOptionPane.OK_OPTION){
					String name = inputName.getText();
					int typeIndex = combo.getSelectedIndex();
					if (name.equals(""))
						JOptionPane.showMessageDialog(MainInterface.instance, "Project must have a name."); 
					else if (typeIndex == -1)
						JOptionPane.showMessageDialog(MainInterface.instance, "You must define a type for the project.");
					else {
						p.setName(inputName.getText());
						p.setDescription(description.getText());
						p.setType((Type)combo.getSelectedItem());
						ManagerSystem.getService().addProject(p);
						throw new InputFormatOKException();
					}
				} else {
					throw new InputFormatOKException();
				}
			}
		} catch (InputFormatOKException ex) {
		}
	}
	
	public String getCurrentName(){
		return inputName.getText();
	}
	
	public String getCurrentDescription(){
		return description.getText();
	}
	
	@SuppressWarnings("serial")
	class InputFormatOKException extends Exception {
		
	}
}
